import { _computeType, _defSymbols, browerEnv } from './view/utils.js';

const linpengKeyBoard = {
    install (Vue) {
        const linpengKeyBoardComponent = require('./linpengKeyBoard.vue').default;
        const InputBox = require('./InputBox.vue').default;
        Vue.component('Iminputdiv', InputBox);

        // Vue.filter('CharCodeIcon', (value) => {
        //   return value && value.toString().replace(/&#\w+;/g, w => String.fromCharCode("0" + w.slice(2, -1)));

        // })
        // 生成一个Vue的子类
        // 同时这个子类也就是组件
        const LindadayoKeyBoardConstructor = Vue.extend(linpengKeyBoardComponent);
        // 生成一个该子类的实例
        const instance = new LindadayoKeyBoardConstructor();
        this.instance = instance;
        // 将这个实例挂载在我创建的div上
        // 并将此div加入全局挂载点内部
        instance.$mount(document.createElement('div'));
        // document.body.appendChild(instance.$el);

        // 移除光标
        Vue.prototype.$closelinpengKeyBoard = (e) => {
            instance.closeclick(e);
        };
        // 增加光标
        Vue.prototype.$addcursor = (vthis, type, event, i, j, k, dir, { symbols = [], boardModel, prevBoardType, boardChoice, fullFirst } = {}) => {
            console.log('$addcursor', prevBoardType);
            instance.vthis = vthis;
            instance.vthis.rootdata.imshow = 1;
            instance.type = type;
            instance.pattern = 'vue';
            instance.boardType = type;
            instance.fullFirst = fullFirst;
            instance.boardModel = boardModel;
            instance.prevBoardType = [];
            // eslint-disable-next-line no-unused-expressions
            prevBoardType ? instance.prevBoardType.push(prevBoardType) : '';
            Object.assign(instance.boardChoice, boardChoice || {});
            instance.show = true;
            instance.inputarray = vthis.inputarray;
            /**/
            if (vthis.testdata.mtype === 38) {
                instance.enterflag = 1;
                instance.i = i;
                instance.maxlength = vthis.maxlength;
            } else if (vthis.testdata.mtype === 4) {
                instance.enterflag = 1;
                instance.i = i;
                instance.maxlength = '';
            } else {
                instance.enterflag = 0;
                instance.maxlength = '';
            }
            instance.addcursor(event, i, j, k, dir, { nextId: vthis.nextId, symbols });
            document.querySelector('body').appendChild(instance.$el);
        };

        // 移除光标
        Vue.prototype.$removecursor = () => {
            instance.removecursor();
        };
        // 移除光标
        if (!Vue.prototype.$toast) {
            Vue.prototype.$toast = instance.$message
        }
    },

    open ({ rightAnswers = [], subject = -1, type = 'none', el = 'body', disableBtnMap = [], change, closed, fillMode, defaultMode, handwriter, beforeChangeType = () => true, lastBoardType = {} } = {}) {
        console.log('open', arguments[0]);
        let instance = this.instance;
        let calcType = '';
        let symbols = _defSymbols(calcType); // 获取默认符号
        let prevBoardType = '';
        let isMobile = browerEnv().android || browerEnv().ios; // 是否移动端
        ({ calcType, symbols, prevBoardType } = _computeType({
            rightAnswers,
            lastBoardType,
            subject,
            boardType: type,
        }));
        instance.jsCallbacks = {
            change,
            closed,
            handwriter,
            beforeChangeType,
        };
        instance.fillMode = fillMode || '';

        if (isMobile) {
            if (calcType === 'handwriter') {
                this.defaultCrossWalkApplay(1);
                defaultMode = 1;
                calcType = 'public';
            } else if (defaultMode) { // 默认打开
                this.defaultCrossWalkApplay(defaultMode);
            } else {
                window.cordova && window.cordova.exec(null, null, 'PenRecog', 'hide', '');
            }
        } else {
            calcType = defaultMode === 1 ? 'handwriter' : calcType;
        }
        console.log('%c 匹配出的键盘类型: ' + calcType, 'color: red');
        if (calcType !== 'public') { // 解决pc默认打开手写键盘，切换公共键盘为空问题
            if (prevBoardType.length < 1) {
                prevBoardType.push('public');
            }
        };
        Object.assign(instance, {
            prevBoardType: prevBoardType,
            disableBtnMap: disableBtnMap,
            symbols: symbols,
            pattern: 'js',
            boardModel: lastBoardType.boardModel || 'chinese',
            show: true,
        });
        // 键盘已经被open过则清空拼音输入后直接启动
        if (instance.boardType !== '') {
            Object.assign(instance, {
                boardType: calcType,
                pinyinstr: '',
            });
            return;
        }

        Object.assign(instance, {
            boardType: calcType,
            isUpper: false,
            el: el, // 键盘的挂载点，默认为body
        });
        document.querySelector(el).appendChild(instance.$el); // 将实例挂载到传入的节点之中
        setTimeout(() => {
            instance.mountedCallback();
        }, 0);
    },
    // 重置并关闭输入法
    reset (e) {
        this.instance.closeclick(e);
        this.instance.boardType = '';
    },

    closed (e) {
        this.instance.closeclick(e);
    },
    defaultCrossWalkApplay (openMode) { // 默认打开类型
        this.instance.crossWalkApplay(openMode);
    },
    openRecoder () {
        this.instance.openRecoder();
    },
};

/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
    linpengKeyBoard.install(window.Vue);
}
export default linpengKeyBoard;
